package com.usefullc.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.domain.DataConfig;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.query.DataConfigQuery;
import com.usefullc.system.service.DataConfigService;
import com.usefullc.system.validator.UsefulValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: Sampson
 * @date: 2022-04-15 15:28
 */
@RestController
@RequestMapping(path = "/system/dataConfig/")
@Api( tags="数据配置管理")
public class DataConfigController extends BaseController {

    @Autowired
    private DataConfigService dataConfigService;


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取列表", response = WebResult.class)
    @GetMapping(path = "list", produces = "application/json")
    public WebResult list(DataConfigQuery query) {
        Pager<DataConfig> pager = dataConfigService.findByPage(query);
        return WebResultBuilder.successResult(pager);
    }

    @ApiOperation(value = "保存字典", response = WebResult.class)
    @PostMapping(path = "save", produces = "application/json")
    public WebResult save(@UsefulValid @RequestBody DataConfig dataConfig) {
        if(dataConfig.getEnable() == null){
            dataConfig.setEnable(true);
        }
        dataConfigService.save(dataConfig);
        return WebResultBuilder.successResult(dataConfig);
    }

    @ApiOperation(value = "修改字典", response = WebResult.class)
    @PostMapping(path = "update", produces = "application/json")
    public WebResult update(@UsefulValid(update = true) @RequestBody DataConfig dataConfig) {
        DataConfig oldBean = dataConfigService.findById(dataConfig.getId());
        BeanUtils.beanCopy(dataConfig,oldBean);
        dataConfigService.save(oldBean);
        return WebResultBuilder.successResult(dataConfig);
    }


    @ApiOperation(value = "删除字典", response = WebResult.class)
    @PostMapping(path = "del", produces = "application/json")
    public WebResult del(@UsefulValid @RequestBody IdsParam idsParam) {
        dataConfigService.deletePhysByIds(idsParam.getIds());
        return WebResultBuilder.successResult();
    }
}
